package com.beiye.drivertransport.safelearn;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.CourseDetailsActivity;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.adapter.CourseSuperAdatper;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.CourseSuperBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.CameraCanUseUtils;
import com.beiye.drivertransport.utils.FileUtil;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.ToastUtil;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.utils.Utils;
import com.githang.statusbar.StatusBarCompat;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

//驾驶员课程超市
 public class CourseSupermarketHomeActivity extends TwoBaseAty {
    private int firstIndex = 1;
    private int pageSize = 10;

    View empty_view;

    LRecyclerView lv_course;

    EditText ed_course;

    TextView tv_search;//搜索

    ImageView img_delet;//清除内容

    ImageView img_safeback;
    private LRecyclerViewAdapter lRecyclerViewAdapter1;
    private CourseSuperAdatper courseSuperAdatper;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};
//    private PopWindowDateApt popWindowDateApt;
//    private PopupWindow mPopWindowDate;
//    ArrayList<CheckDateBean.RowsBean> dateList = new ArrayList<>();

     @Override
     public int getLayoutId() {
        return R.layout.activity_course_supermarket_home;
     }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.img_safeback) {
            finish();
        } else if (id == R.id.tv_course) {
            lv_course.refresh();
            LoginUserBean userInfo = UserManger.getUserInfo();
            LoginUserBean.DataBean data = userInfo.getData();
            if (data == null) {
                return;
            }
            String userId = data.getUserId();
            SharedPreferences sp = getSharedPreferences("CourseSupermarketHomeActivity",
                    Context.MODE_PRIVATE);
            int readInterval = sp.getInt("readInterval", 0);
            String orgId = sp.getString("orgId", "");
            if (orgId.equals("")) {
                lv_course.setEmptyView(empty_view);
                lv_course.refreshComplete(0);
                return;
            }
            String coursename = ed_course.getText().toString().trim();
            if (coursename.equals("")) {
                new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, "", 2, readInterval, firstIndex, pageSize, this, 1);
            } else {
                new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, coursename, 2, readInterval, firstIndex, pageSize, this, 1);
            }
        } else if (id == R.id.img_delet) {
            ed_course.setText("");
            img_delet.setVisibility(View.GONE);
        }
    }
    @Override
    public void initData() {
        empty_view = (View) findViewById(R.id.empty_view);
        lv_course = (LRecyclerView) findViewById(R.id.lv_coursesupermarket);
        ed_course = (EditText) findViewById(R.id.ed_course);
        tv_search = (TextView)findViewById(R.id.tv_course);
        img_delet = (ImageView)findViewById(R.id.img_delet);
        img_safeback = (ImageView) findViewById(R.id.img_safeback);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        //选中公司方法
        initCompany();
        initUi();

        ed_course.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //如果长度为0
                if (charSequence.length() == 0) {
                    img_delet.setVisibility(View.GONE);
                } else {
                    img_delet.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        ed_course.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    ed_course.setCursorVisible(true);// 再次点击显示光标
                }
                return false;
            }
        });
    }
    private void initCompany() {


        Bundle bundle = getIntent().getExtras();
        //通过name关键字取出用户名
        int   readInterval = bundle.getInt("readInterval");
        int   minPer = bundle.getInt("minPer");
        String   orgId = bundle.getString("orgId");
        String   statYm = bundle.getString("statYm");
        String   forbidHour = bundle.getString("forbidHour");
        int  photoMark = bundle.getInt("photoMark");
        int  signMark = bundle.getInt("signMark");
        int    Mark = bundle.getInt("Mark");
        int    actualChLength = bundle.getInt("actualChLength");
        int    setChLength = bundle.getInt("setChLength");
        int    learnsn = bundle.getInt("learnsn");
        int    faceRecgMark = bundle.getInt("faceRecgMark");
        String   photoUrl = bundle.getString("photoUrl");
        int    qpSn = bundle.getInt("qpSn");
        int    allowMuT = bundle.getInt("allowMuT");

        //存储一份readInterval文件
        SharedPreferences sp = getSharedPreferences("CourseSupermarketHomeActivity",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("orgId", orgId);
        editor.putString("statYm", statYm);
        editor.putString("forbidHour", forbidHour);
        editor.putInt("minPer", minPer);
        editor.putInt("readInterval", readInterval);
        editor.putInt("Mark", Mark);
        editor.putInt("photoMark",photoMark);
        editor.putInt("signMark", signMark);
        editor.putInt("actualChLength", actualChLength);
        editor.putInt("setChLength", setChLength);
        editor.putInt("learnsn", learnsn);
        editor.putInt("faceRecgMark", faceRecgMark);
        editor.putString("photoUrl",photoUrl);
        editor.putInt("qpSn", qpSn);
        editor.putInt("allowMuT", allowMuT);
        editor.commit();
        requestData();

    }
    @Override
    public void requestData() {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if (data == null) {
            return;
        }
        String userId = data.getUserId();

        //存储一份readInterval文件
        SharedPreferences sp = getSharedPreferences("CourseSupermarketHomeActivity",
                Context.MODE_PRIVATE);
        int readInterval = sp.getInt("readInterval", 0);
        String orgId = sp.getString("orgId", "");
        if (orgId.equals("")) {
            lv_course.setEmptyView(empty_view);
            lv_course.refreshComplete(0);
            return;
        }
        String coursename = ed_course.getText().toString().trim();
        if (coursename.equals("")) {
            new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, "", 2, readInterval, firstIndex, pageSize, this, 1);
        } else {
            new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, coursename, 2, readInterval, firstIndex, pageSize, this, 1);
        }
    }
    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if (what == 1) {
            CourseSuperBean courseSuperBean = JSON.parseObject(result, CourseSuperBean.class);
            if (courseSuperBean != null) {
                try {
                    if (courseSuperBean.getRows() != null && courseSuperBean.getRows().size() > 0) {
                        lv_course.setVisibility(View.VISIBLE);
                        if (firstIndex == 1) {
                            courseSuperAdatper.clear();
                            courseSuperAdatper.setDataList(courseSuperBean.getRows());
                        } else {
                            courseSuperAdatper.addAll(courseSuperBean.getRows());
                        }

                        if (courseSuperBean.getRows().size() < pageSize) {
                            lv_course.setNoMore(true);
                        }
                    } else {
                        if (firstIndex == 1) {//第一页
                            lv_course.setEmptyView(empty_view);
                            courseSuperAdatper.clear();
                        } else {
                            lv_course.setNoMore(true);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                lv_course.refreshComplete(courseSuperBean.getRows() == null ? 0 : courseSuperBean.getRows().size());
                lRecyclerViewAdapter1.notifyDataSetChanged();
            }
        }
//        else if (what == 2)//校验从业人员已完成的课程是否全部完成
//        {
//
//            SharedPreferences sp = getSharedPreferences("CourseSupermarketHomeActivity",
//                    Context.MODE_PRIVATE);
//            String orgId = sp.getString("orgId", "");
//            String statYm = sp.getString("statYm", "");
//            initspeciacoursesupermaketCheckData(statYm, orgId);//效验指定学习是否学完
//
//        }
       else if (what == 3) {//判断指定学习是否全部学过

            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            String msg = checkBean.getMsg();
            if (code == 0) {
                initcoursesupermaketCheckData();//校验课程超市课程是否学过
            }
        } else if (what == 4) {//校验课程超市课程是否学过
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
           // String msg = checkBean.getMsg();
            if (code == 0) {
                SharedPreferences sp = getSharedPreferences("CourseSupermarketHomeActivity",
                        Context.MODE_PRIVATE);
                int actualChLength = sp.getInt("actualChLength", 0);
                int setChLength = sp.getInt("setChLength", 0);
               final    int qpSn = sp.getInt("qpSn", 0);
               final    int allowMuT = sp.getInt("allowMuT", 0);
                if(actualChLength>=setChLength) {
                    final TiShiDialog.Builder builder = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
                    builder.setMessage("您已完成本月学习任务，继续浏览课程将不记录学时");
                    builder.setTitle("提示:");
                    builder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //存储一份readInterval文件
                            SharedPreferences spp = getSharedPreferences("courseSupermarktcSn1",
                                    Context.MODE_PRIVATE);
                            int tcSn = spp.getInt("tcSn", 0);
                            SharedPreferences sp = getSharedPreferences("CourseSupermarketHomeActivity",
                                    Context.MODE_PRIVATE);
                            //    int readInterval = sp.getInt("readInterval", 0);
                            String orgId = sp.getString("orgId", "");
                            int minPer = sp.getInt("minPer", 0);
                            int photoMark = sp.getInt("photoMark", 0);
                            int signMark = sp.getInt("signMark", 0);
                            int learnsn = sp.getInt("learnsn", 0);
                            int faceRecgMark = sp.getInt("faceRecgMark", 0);
                            String photoUrl = sp.getString("photoUrl", "");
                            Bundle bundle = new Bundle();
                            bundle.putInt("tcsn", tcSn);
                            bundle.putString("orgId", orgId);
                            bundle.putInt("minPer", minPer);
                            bundle.putString("setYm", "");
                            bundle.putInt("photoMark", photoMark);
                            bundle.putInt("signMark", signMark);
                            bundle.putInt("learnsn", learnsn);
                            bundle.putInt("faceRecgMark", faceRecgMark);
                            bundle.putString("photoUrl", photoUrl);
                            bundle.putInt("qpSn", qpSn);
                            startActivity(CourseDetailsActivity.class, bundle);

                        }
                    });

                    builder.setNegativeButton("取消",
                            new android.content.DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });

                    builder.create().show();

                }else {
                    String statYm = sp.getString("statYm", "");
                    SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM");//设置日期格式
                    try {
                        long statymtime = df1.parse(statYm).getTime();
                        long nowtime = df1.parse(df1.format(new Date())).getTime();
                        if(statymtime<nowtime&&allowMuT==0)
                        {
                            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
                            builder1.setMessage("您所在地区不允许补学");
                            builder1.setTitle("提示:");
                            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    //设置你的操作事项
                                }
                            });
                            builder1.create().show();
                            return;
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    //存储一份readInterval文件
                    String orgId = sp.getString("orgId", "");
                    int minPer = sp.getInt("minPer", 0);
                    int photoMark = sp.getInt("photoMark", 0);
                    int signMark = sp.getInt("signMark", 0);
                    int learnsn = sp.getInt("learnsn", 0);
                    int faceRecgMark = sp.getInt("faceRecgMark", 0);
                    String photoUrl = sp.getString("photoUrl", "");
                    SharedPreferences spp = getSharedPreferences("courseSupermarktcSn1",
                            Context.MODE_PRIVATE);
                    int tcSn = spp.getInt("tcSn", 0);
                    Bundle bundle = new Bundle();
                    bundle.putInt("tcsn", tcSn);
                    bundle.putString("orgId", orgId);
                    bundle.putInt("minPer", minPer);
                    bundle.putString("setYm", statYm);
                    bundle.putInt("photoMark", photoMark);
                    bundle.putInt("signMark", signMark);
                    bundle.putInt("learnsn", learnsn);
                    bundle.putInt("faceRecgMark", faceRecgMark);
                    bundle.putString("photoUrl", photoUrl);
                    bundle.putInt("qpSn", qpSn);
                    startActivity(CourseDetailsActivity.class, bundle);
                }

            }
        }
//        else if (what == 4) {
//            CheckDateBean checkDateBean = JSON.parseObject(result, CheckDateBean.class);
//            List<CheckDateBean.RowsBean> rows = checkDateBean.getRows();
//            dateList.clear();
//            dateList.addAll(rows);
//            if (dateList.size() == 0) {
//                if (!CameraCanUseUtils.isCameraCanUse()) {
//                    ToastUtil.showShortToast(getActivity(), "请到手机设置界面里找驾运宝允许开启照相");
//                    return;
//                }
//                final TiShiDialog.Builder builder = new TiShiDialog.Builder(getContext());
//                builder.setMessage("您已完成本月学习任务，继续浏览课程将不记录学时");
//                builder.setTitle("提示:");
//                builder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.dismiss();
//                        //设置你的操作事项
//                        //存储一份readInterval文件
//                        showLoadingDialog("正在加载");
//                        SharedPreferences spp = getActivity().getSharedPreferences("courseSupermarktcSn1",
//                                Context.MODE_PRIVATE);
//                        int tcSn = spp.getInt("tcSn", 0);
//                        Bundle bundle = new Bundle();
//                        bundle.putInt("tcsn", tcSn);
//                        SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
//                                Context.MODE_PRIVATE);
//                        int readInterval = sp.getInt("readInterval", 0);
//                        bundle.putInt("readInterval", readInterval);
//                        //存储一份orgId文件
//                        SharedPreferences sppp = getActivity().getSharedPreferences("orgId4",
//                                Context.MODE_PRIVATE);
//                        String orgId = sppp.getString("orgId", "");
//                        int minPer = sppp.getInt("minPer", 0);
//                        SharedPreferences sp1 = getActivity().getSharedPreferences("photoMarkandsignMark1",
//                                Context.MODE_PRIVATE);
//                        int photoMark = sp1.getInt("photoMark", 0);
//                        int signMark = sp1.getInt("signMark", 0);
//                        bundle.putString("orgId", orgId);
//                        bundle.putInt("minPer", minPer);
//                        bundle.putString("setYm", "");
//                        bundle.putInt("photoMark",photoMark);
//                        bundle.putInt("signMark",signMark);
//                        startActivity(CourseDetailsActivity.class, bundle);
//                    }
//                });
//
//                builder.setNegativeButton("取消",
//                        new android.content.DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int which) {
//                                dialog.dismiss();
//                            }
//                        });
//
//                builder.create().show();
//
//
//            } else {
//
//                popWindowDateApt = new PopWindowDateApt(getContext(), dateList, R.layout.popwindow_dateitem_layout);
//                showPopupWindowDate(dateList);//弹出未完成时
//            }
     //   }


        super.onSuccess(result, call, response, what);


    }
//    private void showPopupWindowDate(List<CheckDateBean.RowsBean> rows) {
//
//        //设置contentView
//        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popwindowdate_layout, null);
//        mPopWindowDate = new PopupWindow(contentView,
//                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
//        //设置各个控件的点击响应
//        LinearLayout view_parent = (LinearLayout) contentView.findViewById(R.id.checkdate_le);
//        ListView lv_date = (ListView) contentView.findViewById(R.id.lv_date);
//        TextView tv_more = (TextView) contentView.findViewById(R.id.tv_checkdate2);
//        TextView tv_back = (TextView) contentView.findViewById(R.id.tv_checkdate4);
//        tv_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mPopWindowDate.dismiss();
//            }
//        });
//        TextView tv_nocheckdate = (TextView) contentView.findViewById(R.id.tv_checkdate3);
//
//        tv_nocheckdate.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//                if (!CameraCanUseUtils.isCameraCanUse()) {
//                    ToastUtil.showShortToast(getActivity(), "请到手机设置界面里找驾运宝允许开启照相");
//                    return;
//                }
//
//                showLoadingDialog("正在加载");
//                //存储一份readInterval文件
//                SharedPreferences spp = getActivity().getSharedPreferences("courseSupermarktcSn1",
//                        Context.MODE_PRIVATE);
//                int tcSn = spp.getInt("tcSn", 0);
//                Bundle bundle = new Bundle();
//                bundle.putInt("tcsn", tcSn);
//                SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
//                        Context.MODE_PRIVATE);
//                int readInterval = sp.getInt("readInterval", 0);
//                bundle.putInt("readInterval", readInterval);
//                //存储一份orgId文件
//                SharedPreferences sppp = getActivity().getSharedPreferences("orgId4",
//                        Context.MODE_PRIVATE);
//                String orgId = sppp.getString("orgId", "");
//                int minPer = sppp.getInt("minPer", 0);
//                SharedPreferences sp1 = getActivity().getSharedPreferences("photoMarkandsignMark1",
//                        Context.MODE_PRIVATE);
//                int photoMark = sp1.getInt("photoMark", 0);
//                int signMark = sp1.getInt("signMark", 0);
//                bundle.putString("orgId", orgId);
//                bundle.putInt("minPer", minPer);
//                bundle.putString("setYm", "");
//                bundle.putInt("photoMark", photoMark);
//                bundle.putInt("signMark", signMark);
//                startActivity(CourseDetailsActivity.class, bundle);
//                mPopWindowDate.dismiss();
//            }
//        });
//        mPopWindowDate.showAtLocation(view_parent, Gravity.CENTER, 0, 0);
//        if (rows.size() >= 3) {
//            View listItem = popWindowDateApt.getView(0, null, lv_date);
//            listItem.measure(0, 0);
//            int totalHei = (listItem.getMeasuredHeight() + lv_date.getDividerHeight()) * 3;
//            lv_date.getLayoutParams().height = totalHei;
//            lv_date.setAdapter(popWindowDateApt);
//            tv_more.setVisibility(View.VISIBLE);
//        } else {
//            lv_date.setAdapter(popWindowDateApt);
//            tv_more.setVisibility(View.INVISIBLE);
//        }
//
//        lv_date.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//
//                if (dateList.size() >= 1) {
//                    if (position == 0) {
//                        showLoadingDialog("正在加载");
//                        String setYm = popWindowDateApt.getItem(0).getSetYm();
//                        SharedPreferences sp = getActivity().getSharedPreferences("orgId4",
//                                Context.MODE_PRIVATE);
//                        SharedPreferences.Editor edit = sp.edit();
//                        edit.putString("setYm", setYm);
//                        edit.commit();
//                        String orgId = sp.getString("orgId", "");
//                        initspeciacoursesupermaketCheckData(setYm, orgId);
//                    } else {
//
//                        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
//                        builder1.setMessage("请先学习上月或以往未完成的课程");
//                        builder1.setTitle("提示:");
//                        builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int which) {
//                                dialog.dismiss();
//                                ;
//                                //设置你的操作事项
//
//                            }
//                        });
//                        builder1.create().show();
//                    }
//                }
//            }
//        });
//
//    }

    //补学弹窗校验
//    private void initcoursesupermaketCheckDateData() {
//
//        //存储一份orgId文件
//        SharedPreferences spp = getActivity().getSharedPreferences("orgId4",
//                Context.MODE_PRIVATE);
//        String orgId = spp.getString("orgId", "");
//        LoginUserBean userInfo = UserManger.getUserInfo();
//        LoginUserBean.DataBean data = userInfo.getData();
//        if (data == null) {
//            return;
//        }
//        String userId = data.getUserId();
//        new Login().getcoursesupermaketCheckDate(userId, orgId, this, 4);
//
//    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what == 3) {//判断指定学习是否全部学过

            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            if (code == 2813) {
                String msg = checkBean.getMsg();
                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
                builder1.setMessage(msg);
                builder1.setTitle("提示:");
                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        ;
                        //设置你的操作事项
                        //mPopWindowDate.dismiss();
                    }
                });
                builder1.create().show();

            }
        } else if (what == 4) {//判断课程是否学完
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            if (code == 1812) {
                String msg = checkBean.getMsg();
                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
                builder1.setMessage(msg);
                builder1.setTitle("提示:");
                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        ;
                        //设置你的操作事项
                    }
                });
                builder1.create().show();
            }
        }
//        else if(what==2)
//        {
//            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
//            String msg = checkBean.getMsg();
//            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
//            builder1.setMessage(msg);
//            builder1.setTitle("提示:");
//            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
//                public void onClick(DialogInterface dialog, int which) {
//                    dialog.dismiss();
//                    ;
//                    //设置你的操作事项
//                }
//            });
//            builder1.create().show();
//        }
        super.onError(result, call, response, what);
    }

    @Override
    public void onResume() {
        super.onResume();
        lv_course.refresh();
        requestData();
    }
    private void initUi() {
        LinearLayoutManager ms = new LinearLayoutManager(CourseSupermarketHomeActivity.this);
        ms.setOrientation(LinearLayoutManager.VERTICAL);
        lv_course.setLayoutManager(ms);
        courseSuperAdatper = new CourseSuperAdatper(CourseSupermarketHomeActivity.this);
        lRecyclerViewAdapter1 = new LRecyclerViewAdapter(courseSuperAdatper);
        lv_course.setAdapter(lRecyclerViewAdapter1);
        lv_course.setFooterViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        lv_course.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        lv_course.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {
                firstIndex = 1;
                requestData();
            }
        });

        lv_course.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore() {
                firstIndex += pageSize;
                requestData();
            }
        });
        empty_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lv_course.refresh();
            }
        });

        lRecyclerViewAdapter1.setOnItemClickListener(new com.github.jdsjlzx.interfaces.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (Utils.isFastClicker()) {
                    return;
                }
                SharedPreferences sp1 = getSharedPreferences("CourseSupermarketHomeActivity", Context.MODE_PRIVATE);
                String forbidHour = sp1.getString("forbidHour", "");
                String orgId1 = sp1.getString("orgId", "");
                String statYm1 = sp1.getString("statYm", "");
                if(!forbidHour.equals("")) {
                    String s1 = forbidHour.split(",")[0];
                    String s2 = forbidHour.split(",")[1];
                    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
                    //初始化
                    Date nowTime = null;
                    Date beginTime = null;
                    Date endTime = null;
                    if (s1.length() == 2) {
                        if(Integer.parseInt(s1)<=Integer.parseInt(s2))
                        {
                            if (s1.length() == 1) {
                                try {

                                    //定义开始时间
                                    beginTime = df.parse("0" + s1 + ":00"+ ":00");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                try {
                                    //定义开始时间
                                    beginTime = df.parse(s1 + ":00"+ ":00");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            if (s2.length() == 1) {
                                try {

                                    //定义开始时间
                                    endTime = df.parse("0" + s2 + ":59"+ ":59");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                try {
                                    //定义开始时间
                                    endTime = df.parse(s2 + ":59"+ ":59");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            try {
                                //格式化当前时间格式
                                nowTime = df.parse(df.format(new Date()));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            //调用判断方法
                            boolean flag = FileUtil.belongCalendar(nowTime, beginTime, endTime);
                            if (flag) {
                                //处于规定的时间段内，执行对应的逻辑代码
                                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
                                builder1.setMessage("安全第一，现在是休息时间，明天再来学吧！");
                                builder1.setTitle("提示:");
                                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        //设置你的操作事项
                                    }
                                });
                                builder1.create().show();

                                return;

                            }
                        }else{

                            if (s2.length() == 1) {
                                try {
                                    //定义开始时间
                                    beginTime = df.parse("0" + s2 + ":00"+ ":00");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                try {
                                    //定义开始时间
                                    beginTime = df.parse(s2 + ":00"+ ":00");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            if (s1.length() == 1) {
                                try {

                                    //定义开始时间
                                    endTime = df.parse("0" + s1 + ":59"+ ":59");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                try {
                                    //定义开始时间
                                    endTime = df.parse(s1 + ":59"+ ":59");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            try {
                                //格式化当前时间格式
                                nowTime = df.parse(df.format(new Date()));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            //调用判断方法
                            boolean flag = FileUtil.belongCalendar(nowTime, beginTime, endTime);
                            if (!flag) {
                                //处于规定的时间段内，执行对应的逻辑代码
                                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
                                builder1.setMessage("安全第一，现在是休息时间，明天再来学吧！");
                                builder1.setTitle("提示:");
                                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        //设置你的操作事项
                                    }
                                });
                                builder1.create().show();

                                return;

                            }
                        }
                    }  else {
                        if (s1.length() == 1) {
                            try {

                                //定义开始时间
                                beginTime = df.parse("0" + s1 + ":00"+ ":00");

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            try {
                                //定义开始时间
                                beginTime = df.parse(s1 + ":00"+ ":00");

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        if (s2.length() == 1) {
                            try {

                                //定义开始时间
                                endTime = df.parse("0" + s2 + ":59"+ ":59");

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            try {
                                //定义开始时间
                                endTime = df.parse(s2 + ":59"+ ":59");

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            //格式化当前时间格式
                            nowTime = df.parse(df.format(new Date()));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        //调用判断方法
                        boolean flag = FileUtil.belongCalendar(nowTime, beginTime, endTime);
                        if (flag) {
                            //处于规定的时间段内，执行对应的逻辑代码
                            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
                            builder1.setMessage("安全第一，现在是休息时间，明天再来学吧！");
                            builder1.setTitle("提示:");
                            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    //设置你的操作事项
                                }
                            });
                            builder1.create().show();

                            return;

                        }

                    }
                    //检测是否有写的权限
                    int permission = ActivityCompat.checkSelfPermission(CourseSupermarketHomeActivity.this,
                            "android.permission.WRITE_EXTERNAL_STORAGE");
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        // 没有写的权限，去申请写的权限，会弹出对话框
                        ActivityCompat.requestPermissions(CourseSupermarketHomeActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                        return;
                    }


                    if (!CameraCanUseUtils.isCameraCanUse()) {
                        ToastUtil.showShortToast(CourseSupermarketHomeActivity.this, "请到手机设置界面里找驾运宝允许开启照相");
                        return;
                    }
                    try {
//                        int mark = sp1.getInt("Mark", 1);
//                        if (mark == 0) {
//                            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
//                            builder1.setMessage("您已被企业管理员禁用,请联系企业管理员");
//                            builder1.setTitle("提示:");
//                            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
//                                public void onClick(DialogInterface dialog, int which) {
//                                    dialog.dismiss();
//                                    //设置你的操作事项
//                                }
//                            });
//                            builder1.create().show();
//                            return;
//                        }
                        //校验是否学过课程
                        int tcSn = courseSuperAdatper.getDataList().get(position).getTcSn();
                        //存储一份tcSn文件
                        SharedPreferences sp = getSharedPreferences("courseSupermarktcSn1",
                                Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putInt("tcSn", tcSn);
                        editor.commit();

                        initspeciacoursesupermaketCheckData(statYm1,orgId1);//校验指定学习是否学过

//                        LoginUserBean userInfo = UserManger.getUserInfo();
//                        String userId1 = userInfo.getData().getUserId();
//                        initLearningCompleData(userId1, orgId1, statYm1);//校验从业人员已完成的课程是否全部完成


           //             initcoursesupermaketCheckData();//校验课程超市课程是否学过

                    } catch (Exception e) {
                        e.printStackTrace();
                    }



                }else {

                    //检测是否有写的权限
                    int permission = ActivityCompat.checkSelfPermission(CourseSupermarketHomeActivity.this,
                            "android.permission.WRITE_EXTERNAL_STORAGE");
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        // 没有写的权限，去申请写的权限，会弹出对话框
                        ActivityCompat.requestPermissions(CourseSupermarketHomeActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                        return;
                    }


                    if (!CameraCanUseUtils.isCameraCanUse()) {
                        ToastUtil.showShortToast(CourseSupermarketHomeActivity.this, "请到手机设置界面里找驾运宝允许开启照相");
                        return;
                    }
                    try {
//                        int mark = sp1.getInt("Mark", 1);
//                        if (mark == 0) {
//                            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(CourseSupermarketHomeActivity.this);
//                            builder1.setMessage("您已被企业管理员禁用,请联系企业管理员");
//                            builder1.setTitle("提示:");
//                            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
//                                public void onClick(DialogInterface dialog, int which) {
//                                    dialog.dismiss();
//                                    //设置你的操作事项
//                                }
//                            });
//                            builder1.create().show();
//                            return;
//                        }
                        //校验是否学过课程
                        int tcSn = courseSuperAdatper.getDataList().get(position).getTcSn();
                        //存储一份tcSn文件
                        SharedPreferences sp = getSharedPreferences("courseSupermarktcSn1",
                                Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putInt("tcSn", tcSn);
                        editor.commit();
                        initspeciacoursesupermaketCheckData(statYm1,orgId1);//校验指定学习是否学过

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        lv_course.refresh();

    }

//    private void initLearningCompleData(String userId1, String orgId1, String statYm1) {
//        new Login().SpecifiedLearningcourseComple(userId1, orgId1, statYm1, this, 2);
//    }


    //校验指定学习是否学过
    private void initspeciacoursesupermaketCheckData(String statYm, String orgId) {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if (data == null) {
            return;
        }
        String userId = data.getUserId();
        new Login().getlearncoursesupermaketCheck(userId, orgId, statYm, this, 3);
    }

    //    //校验课程超市课程是否学过
    private void initcoursesupermaketCheckData() {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if (data == null) {
            return;
        }
        String userId = data.getUserId();
        //存储一份readInterval文件
        SharedPreferences sp = getSharedPreferences("CourseSupermarketHomeActivity",
                Context.MODE_PRIVATE);
        int readInterval = sp.getInt("readInterval", 0);
        //存储一份orgId文件
        String orgId = sp.getString("orgId", "");
        //存储一份readInterval文件
        SharedPreferences spp = getSharedPreferences("courseSupermarktcSn1",
                Context.MODE_PRIVATE);
        int tcSn = spp.getInt("tcSn", 0);
//        dialog = CustomProgressDialog.show(getContext(), "", true, null);
        new Login().getPreJobTrainingandcoursesupermaketCheck(userId, orgId, tcSn, readInterval, this, 4);

    }
}
